package com.wyd.note.business.dict;

import com.wyd.common.bean.ResultMy;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.text.ParseException;

@Api("Dict词典")
@RequestMapping("/dict")
@RestController
public class DictController {
    private final DictService dictService;
    private final DictRepository dictRepository;

//    @Autowired 官方推荐 构造注入 或者 set注入 如果只有一个构造函数可以省略
    public DictController(DictService dictService, DictRepository dictRepository) {
        this.dictService = dictService;
        this.dictRepository = dictRepository;
    }

    @PermitAll
    @PostMapping
    public ResultMy<Dict> create(@RequestBody @Validated Dict dict) {
        return ResultMy.build("0", "", dictService.create(dict));
    }

    @PostMapping("/public/")
    public ResultMy<Dict> create2(@RequestBody @Validated Dict dict) {
        return ResultMy.build("0", "", this.dictService.create(dict));
    }

    @PostMapping("/public/{id}")
    public ResultMy<Dict> update(@PathVariable String id, @RequestBody @Validated Dict dict) {
        return ResultMy.build("0", "", this.dictService.update(id, dict));
    }

    @GetMapping("/public/test")
    public ResultMy<Object> test() {
        return ResultMy.build("0", "", dictRepository.search());
    }

    @GetMapping("/public/test2")
    public ResultMy<Object> test2() {
        return ResultMy.build("0", "", dictService.search());
    }

    @GetMapping("/public/test3")
    public ResultMy<Object> test3() throws ParseException {
        return ResultMy.build("0", "", dictService.querydsl());
    }
}
